# My name is application image B, my artifacts are available for you:
# name: {{ application_b['name']}}
# binary path: {{ application_b['binary']}}
# suit envelope: {{ artifacts_folder ~ application_b['name'] }}.suit
# My name is radio image B, my artifacts are available for you:
# name: {{ radio_b['name']}}
# binary path: {{ radio_b['binary']}}
# suit envelope: {{ artifacts_folder ~ radio_b['name'] }}.suit

{%- set mpi_app_recovery_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_RECOVERY_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_app_recovery_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_RECOVERY_CLASS_NAME']|default('nRF54H20_app_recovery') %}
{%- set mpi_application_a_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_1_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_application_a_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_1_CLASS_NAME']|default('nRF54H20_sample_app') %}
{%- set mpi_application_b_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_2_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_application_b_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_2_CLASS_NAME']|default('nRF54H20_sample_app_local_2') %}
{%- set mpi_radio_a_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_1_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_radio_a_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_1_CLASS_NAME']|default('nRF54H20_sample_rad') %}
{%- set mpi_radio_b_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_2_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_radio_b_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_LOCAL_2_CLASS_NAME']|default('nRF54H20_sample_rad_local_2') %}

SUIT_Envelope_Tagged:
  suit-authentication-wrapper:
    SuitDigest:
      suit-digest-algorithm-id: cose-alg-sha-256
  suit-manifest:
    suit-manifest-version: 1
{%- if APP_ROOT_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ APP_ROOT_SEQ_NUM }}
{%- elif DEFAULT_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ DEFAULT_SEQ_NUM }}
{%- else %}
    suit-manifest-sequence-number: 1
{%- endif %}

    suit-common:
      suit-components:

      {%- set component_index = 0 %}
      {%- set CAND_MFST_IDX = component_index %}
      - - CAND_MFST
        - 0

      {%- set BOOT_STATUS_A = 1 %}
      {%- set BOOT_STATUS_B = 2 %}
      {%- set BOOT_STATUS_A_DEGRADED = 3 %}
      {%- set BOOT_STATUS_B_DEGRADED = 4 %}
      {%- set BOOT_STATUS_A_NO_RADIO = 5 %}
      {%- set BOOT_STATUS_B_NO_RADIO = 6 %}
      {%- set BOOT_STATUS_CANNOT_BOOT = 7 %}
      {%- set component_index = component_index + 1 %}
      {%- set VAR_BOOT_STATUS_IDX = component_index %}
      - - MFST_VAR
        - 256

      {%- set JUST_INSTALLED = 1 %}
      {%- set NOT_CONFIRMED = 2 %}
      {%- set CONFIRMED = 3 %}
      # Any other value - JUST_INSTALLED
      {%- set component_index = component_index + 1 %}
      {%- set VAR_CONFIRM_SET_A_IDX = component_index %}
      - - MFST_VAR
        - 2

      {%- set component_index = component_index + 1 %}
      {%- set VAR_CONFIRM_SET_B_IDX = component_index %}
      - - MFST_VAR
        - 3

      {%- set component_index = component_index + 1 %}
      {%- set RAD_A_INSTLD_MFST_IDX = component_index %}
      - - INSTLD_MFST
        - RFC4122_UUID:
            namespace: {{ mpi_radio_a_vendor_name }}
            name: {{ mpi_radio_a_class_name }}

      {%- set component_index = component_index + 1 %}
      {%- set RAD_B_INSTLD_MFST_IDX = component_index %}
      - - INSTLD_MFST
        - RFC4122_UUID:
            namespace: {{ mpi_radio_b_vendor_name }}
            name: {{ mpi_radio_b_class_name }}

      {%- set component_index = component_index + 1 %}
      {%- set APP_A_INSTLD_MFST_IDX = component_index %}
      - - INSTLD_MFST
        - RFC4122_UUID:
            namespace: {{ mpi_application_a_vendor_name }}
            name: {{ mpi_application_a_class_name }}

      {%- set component_index = component_index + 1 %}
      {%- set APP_B_INSTLD_MFST_IDX = component_index %}
      - - INSTLD_MFST
        - RFC4122_UUID:
            namespace: {{ mpi_application_b_vendor_name }}
            name: {{ mpi_application_b_class_name }}

      suit-shared-sequence:
      - suit-directive-set-component-index: [{{ RAD_A_INSTLD_MFST_IDX }}, {{ RAD_B_INSTLD_MFST_IDX }}, {{ APP_A_INSTLD_MFST_IDX }}, {{ APP_B_INSTLD_MFST_IDX }}]
      - suit-directive-override-parameters:
          suit-parameter-vendor-identifier:
            RFC4122_UUID: {{ mpi_app_recovery_vendor_name }}
          suit-parameter-class-identifier:
            RFC4122_UUID:
              namespace: {{ mpi_app_recovery_vendor_name }}
              name: {{ mpi_app_recovery_class_name }}
      - suit-condition-vendor-identifier: []
      - suit-condition-class-identifier: []

      suit-dependencies:
        # Key is the index of suit-components that describe the dependency manifest
        "{{ CAND_MFST_IDX }}": {}
        "{{ RAD_A_INSTLD_MFST_IDX }}": {}
        "{{ RAD_B_INSTLD_MFST_IDX }}": {}
        "{{ APP_A_INSTLD_MFST_IDX }}": {}
        "{{ APP_B_INSTLD_MFST_IDX }}": {}


    suit-validate:
    - suit-directive-set-component-index: {{ APP_A_INSTLD_MFST_IDX }}
    - suit-directive-try-each:

      # Checking if A is confirmed
      - - suit-directive-set-component-index: {{ VAR_CONFIRM_SET_A_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ CONFIRMED }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_A_DEGRADED }}
        - suit-directive-write: []
        # A 'confirm' status allows booting, validating app and radio
        - suit-directive-set-component-index: [{{ APP_A_INSTLD_MFST_IDX }}, {{ RAD_A_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []

      # Checking if B is confirmed
      - - suit-directive-set-component-index: {{ VAR_CONFIRM_SET_B_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ CONFIRMED }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_B_DEGRADED }}
        - suit-directive-write: []
        # B 'confirm' status allows booting, validating app and radio
        - suit-directive-set-component-index: [{{ APP_B_INSTLD_MFST_IDX }}, {{ RAD_B_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []

      # Bad - neither A or B is confirmed
      # Try to find ANY set, where both app and radio are valid
      # As we are already in recovery mode, report the boot status as degraded.
      # Checking A
      - - suit-directive-set-component-index: [{{ APP_A_INSTLD_MFST_IDX }}, {{ RAD_A_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_A_DEGRADED }}
        - suit-directive-write: []

      # Checking B
      - - suit-directive-set-component-index: [{{ APP_B_INSTLD_MFST_IDX }}, {{ RAD_B_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_B_DEGRADED }}
        - suit-directive-write: []

      # Worse. Try to find valid app
      # Checking A
      - - suit-directive-set-component-index: {{ APP_A_INSTLD_MFST_IDX }}
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_A_NO_RADIO }}
        - suit-directive-write: []
        # Still run validate for the radio manifest, only in order to set
        # the inactive radio partition as IPUC.
        # The sequence will fail, but due to the soft-failure parameter
        # it will not cause the whole current try-each sequence failure.
        - suit-directive-run-sequence:
          - suit-directive-set-component-index: {{ RAD_A_INSTLD_MFST_IDX }}
          - suit-directive-override-parameters:
              suit-parameter-soft-failure: True
          - suit-condition-dependency-integrity: []
          - suit-directive-process-dependency: []

      # Checking B
      - - suit-directive-set-component-index: {{ APP_B_INSTLD_MFST_IDX }}
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []
        - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_B_NO_RADIO }}
        - suit-directive-write: []
        # Still run validate for the radio manifest, only in order to set
        # the inactive radio partition as IPUC.
        # The sequence will fail, but due to the soft-failure parameter
        # it will not cause the whole current try-each sequence failure.
        - suit-directive-run-sequence:
          - suit-directive-set-component-index: {{ RAD_B_INSTLD_MFST_IDX }}
          - suit-directive-override-parameters:
              suit-parameter-soft-failure: True
          - suit-condition-dependency-integrity: []
          - suit-directive-process-dependency: []


    suit-invoke:
    - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
    - suit-directive-try-each:

      # Path A, degraded
      - - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_A_DEGRADED }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: [{{ APP_A_INSTLD_MFST_IDX }}, {{ RAD_A_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []

      # Path B, degraded
      - - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_B_DEGRADED }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: [{{ APP_B_INSTLD_MFST_IDX }}, {{ RAD_B_INSTLD_MFST_IDX }}]
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []

      # Path A, just app image
      - - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_A_NO_RADIO }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: {{ APP_A_INSTLD_MFST_IDX }}
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []

      # Path B, just app image
      - - suit-directive-set-component-index: {{ VAR_BOOT_STATUS_IDX }}
        - suit-directive-override-parameters:
            suit-parameter-content: {{ BOOT_STATUS_B_NO_RADIO }}
        - suit-condition-check-content: []
        - suit-directive-set-component-index: {{ APP_B_INSTLD_MFST_IDX }}
        - suit-condition-dependency-integrity: []
        - suit-directive-process-dependency: []


{%- if APP_ROOT_VERSION is defined %}
    suit-current-version: {{ APP_ROOT_VERSION }}
{%- elif DEFAULT_VERSION is defined %}
    suit-current-version: {{ DEFAULT_VERSION }}
{%- endif %}


    suit-install:
    # Do nothing, only the manifest needs to be installed (done automatically).
    - suit-directive-set-component-index: 0

    suit-manifest-component-id:
    - INSTLD_MFST
    - RFC4122_UUID:
        namespace: {{ mpi_app_recovery_vendor_name }}
        name: {{ mpi_app_recovery_class_name }}
